package com.zhengdongzulin.zuke.app.main.mine.lifeservice

import android.view.View
import com.lxj.androidktx.core.*
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.LifeServiceDetails
import com.zhengdongzulin.zuke.base.BaseActivity
import com.zhengdongzulin.zuke.databinding.ZkActivityLifeServiceDetailsBinding
import com.zhengdongzulin.zuke.ext.loadImage


class ZKLifeServiceDetailsActivity :
    BaseActivity<LifeServiceDetailsVm, ZkActivityLifeServiceDetailsBinding>() {

    lateinit var mId: String
    lateinit var mType: String  //保洁    维修    投诉
    lateinit var mData: LifeServiceDetails

    override fun getLayoutResId(): Int = R.layout.zk_activity_life_service_details

    override fun initVM(): LifeServiceDetailsVm = getVM(LifeServiceDetailsVm::class.java)

    override fun initView() {
        mId = intent.getStringExtra("id")!!
        mType = intent.getStringExtra("type")!!
        setTitleContent(mType + "详情")
        mView.statusView.showLoading()
    }

    override fun initData() {
        when (mType) {
            "保洁" -> {
                mViewModel.get_clean_detail(mId)
            }
            "维修" -> {
                mViewModel.get_repair_detail(mId)
            }
            "投诉" -> {
                mView.tvTimeTitle.text = "提交时间"
                mViewModel.get_complaint_letter(mId)
            }
        }

    }


    override fun initListener() {

    }

    override fun startObserve() {
        mViewModel.get_clean_detail.observe(this, androidx.lifecycle.Observer {
            mData = it
            showView()
        })
        mViewModel.get_clean_detail.state.observe(this, getObserver(mViewModel.get_clean_detail))

        mViewModel.get_repair_detail.observe(this, androidx.lifecycle.Observer {
            mData = it
            showView()
        })
        mViewModel.get_repair_detail.state.observe(this, getObserver(mViewModel.get_repair_detail))

        mViewModel.get_complaint_letter.observe(this, androidx.lifecycle.Observer {
            mData = it
            showView()
        })
        mViewModel.get_complaint_letter.state.observe(
            this,
            getObserver(mViewModel.get_complaint_letter)
        )
    }

    private fun showView() {
        mView.tvLifeServiceDetailsStatus.text = getStatusStr(mData.status)
        mView.tvLifeServiceDetailsAddress.text = "地址：${mData.houseAddress}"
        mView.tvLifeServiceDetailsContent.text = mData.repairServiceContent

        if (!mData.expectCompletionTime.isNullOrEmpty()) {
            mView.tvLifeServiceDetailsTime.text = "：${mData.expectCompletionTime}"
            mView.tvLifeServiceDetailsTime.visibility = View.VISIBLE
        } else {
            mView.tvLifeServiceDetailsTime.visibility = View.GONE
        }

        if (!mData.repairDetail.isNullOrEmpty()) {
            mView.tvLifeServiceDetailsRepairDetail.text = mData.repairDetail
            mView.tvLifeServiceDetailsRepairDetail.visibility = View.VISIBLE
        } else {
            mView.tvLifeServiceDetailsRepairDetail.visibility = View.GONE
        }

        if (!mData.grade.isNullOrEmpty()) {
            mView.rbLifeServiceDetailsRatingBar.isEnabled = false
            mView.rbLifeServiceDetailsRatingBar.rating = mData.grade.toFloat()
            mView.tvLifeServiceDetailsGradeContent.text = mData.gradeContent
            mView.llLifeServiceDetailsRepairRating.visibility = View.VISIBLE
        } else {
            mView.llLifeServiceDetailsRepairRating.visibility = View.GONE
        }
        if (mData.renterUploadPicList != null && mData.renterUploadPicList.size > 0) {
            mView.ivEmpty.gone()
            mView.recyclerView.visible()
            mView.recyclerView.vertical(3).bindData(
                mData.renterUploadPicList,
                R.layout.item_feedback
            ) { holder, t, position ->
                holder.loadImage(R.id.img, t.url, 0, 2.0f)
                holder.getView<View>(R.id.remove).gone()
            }
        } else {
            mView.ivEmpty.visible()
            mView.recyclerView.gone()
        }

        mView.statusView.showContent()
    }

    /**
     * 获取status对应的中文
     */
    private fun getStatusStr(status: String): String {
        when (status) {
            "1" -> {
                return "待处理"
            }
            "2" -> {
                return "已派单"
            }
            "3" -> {
                return "待验收"
            }
            "4" -> {
                return "已取消"
            }
            "5" -> {
                return "已验收"
            }
        }
        return status
    }


}